package com.wangzifan.controller;

import com.wangzifan.entity.Grade;
import com.wangzifan.entity.PageBean;
import com.wangzifan.entity.Result;
import com.wangzifan.service.GradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/grades")
public class GradeController {
    @Autowired
    private GradeService gradeService;

    /**
     * 查询全部学生成绩
     * @return
     */
    @GetMapping
    public Result queryGrade(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             String sno, String cno) {
        log.info("分页查询, 参数: {},{},{}",page,pageSize,sno,cno);
        PageBean pageBean = gradeService.page(page,pageSize,sno,cno);
        return Result.success(pageBean);
    }

    /**
     * 根据id查询成绩
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryGradeById(@PathVariable Integer id) {
        Grade grade = gradeService.queryGradeById(id);
        return Result.success(grade);
    }

    @GetMapping("/teacher")
    public Result queryGradeByTeacher(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                      String tno, String cno) {
        log.info("分页查询, 参数: {},{},{},{}",page,pageSize,tno,cno);
        PageBean pageBean = gradeService.pageByTeacher(page,pageSize,tno,cno);
        return Result.success(pageBean);
    }

    /**
     * 批量删除学生成绩
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteGrade(@PathVariable List<Integer> ids) {
        gradeService.deleteGrade(ids);
        return Result.success();
    }

    /**
     * 添加学生成绩
     * @param grade
     * @return
     */
    @PostMapping()
    public Result addGrade(@RequestBody Grade grade) {
        gradeService.addGrade(grade);
        return Result.success();
    }

    /**
     * 更新学生成绩
     * @param grade
     * @return
     */
    @PutMapping()
    public Result updateGrade(@RequestBody Grade grade) {
        gradeService.updateGrade(grade);
        return Result.success();
    }



}
